package services;

import java.util.List;

import javax.jdo.PersistenceManager;

import javax.jdo.Query;

import com.sun.org.apache.regexp.internal.recompile;

import datastore.Contact;
import datastore.PMF;
import datastore.ParticipantTags;

public class DBService {
	
	 public void storeParticipantTags(String email,String emailContact,String tags){
		 ParticipantTags participantTags = new ParticipantTags( email, emailContact, tags);
		 
		 PersistenceManager pm = PMF.get().getPersistenceManager();
	     try {
	         pm.makePersistent(participantTags);
	     } finally {
	         pm.close();
	     }

		 
	 }
	 
	 public List<ParticipantTags> getParticipantTags(String email){
		 
		 PersistenceManager pm = PMF.get().getPersistenceManager();		 
		  
		 Query query = pm.newQuery(ParticipantTags.class,"email == emailParam");
	     query.declareParameters("String emailParam");

	     List<ParticipantTags> results = null;
	     
	     try {
	         results = (List<ParticipantTags>) query.execute(email);	         
	     } finally {
	         query.closeAll();
	     }
	     
	    
		 
		 return results;
	 }

	
	 public void storeContactToDatabase(String email, String password){
		 
		 Contact contact = new Contact(email,password);
	     
	     
	     System.out.println("Store contact to database");
	     
	     PersistenceManager pm = PMF.get().getPersistenceManager();
	     try {
	         pm.makePersistent(contact);
	     } finally {
	         pm.close();
	     }

	 }
	 
	 public Contact getContact(String email){
		 
		 PersistenceManager pm = PMF.get().getPersistenceManager();
		 
		  
		 Query query = pm.newQuery(Contact.class,"email == emailParam");
	     query.declareParameters("String emailParam");

	     List<Contact> results = null;
	     
	     try {
	         results = (List<Contact>) query.execute(email);	         
	     } finally {
	         query.closeAll();
	     }
	     
	    System.out.println(results.size());
		 
	     if(results.size() == 0){
	    	 return null;
	     }
	     
		 return results.get(0);
	 }
	
}
